From: Brian Wolff Date: Thu, 8 Sep 2016 23:43:36 +0000 (+0000) Subject: API: When undoing an edit, allow overriding content model. X-Git-Tag: 1.31.0-rc.0~5690^2 X-Git-Url: http://git.cyclocoop.org//%22http:/%22.attribut_html%28%24lesurls%5B%24numero%5D%29.%22/%22?a=commitdiff_plain;h=99485235b0be6a55337c3d26fc4b4c594a5c5e97;p=lhc%2Fweb%2Fwiklou.git API: When undoing an edit, allow overriding content model. This brings the API in line with web UI changes from Ic528f65d. Bug: T145044 Change-Id: Ib97eef38d228c4da4b062ee96ddbbbb926ee665b --- diff --git a/includes/api/ApiEditPage.php b/includes/api/ApiEditPage.php index 00daba94b5..ee9150cc86 100644 --- a/includes/api/ApiEditPage.php +++ b/includes/api/ApiEditPage.php @@ -97,6 +97,7 @@ class ApiEditPage extends ApiBase { } else { $contentHandler = ContentHandler::getForModelID( $params['contentmodel'] ); } + $contentModel = $contentHandler->getModelID(); $name = $titleObj->getPrefixedDBkey(); $model = $contentHandler->getModelID(); @@ -111,11 +112,11 @@ class ApiEditPage extends ApiBase { } if ( !isset( $params['contentformat'] ) || $params['contentformat'] == '' ) { - $params['contentformat'] = $contentHandler->getDefaultFormat(); + $contentFormat = $contentHandler->getDefaultFormat(); + } else { + $contentFormat = $params['contentformat']; } - $contentFormat = $params['contentformat']; - if ( !$contentHandler->isSupportedFormat( $contentFormat ) ) { $this->dieUsage( "The requested format $contentFormat is not supported for content model " . @@ -265,9 +266,21 @@ class ApiEditPage extends ApiBase { if ( !$newContent ) { $this->dieUsageMsg( 'undo-failure' ); } - - $params['text'] = $newContent->serialize( $params['contentformat'] ); - + if ( empty( $params['contentmodel'] ) + && empty( $params['contentformat'] ) + ) { + // If we are reverting content model, the new content model + // might not support the current serialization format, in + // which case go back to the old serialization format, + // but only if the user hasn't specified a format/model + // parameter. + if ( !$newContent->isSupportedFormat( $contentFormat ) ) { + $contentFormat = $undoafterRev->getContentFormat(); + } + // Override content model with model of undid revision. + $contentModel = $newContent->getModel(); + } + $params['text'] = $newContent->serialize( $contentFormat ); // If no summary was given and we only undid one rev, // use an autosummary if ( is_null( $params['summary'] ) && @@ -288,7 +301,7 @@ class ApiEditPage extends ApiBase { $requestArray = [ 'wpTextbox1' => $params['text'], 'format' => $contentFormat, - 'model' => $contentHandler->getModelID(), + 'model' => $contentModel, 'wpEditToken' => $params['token'], 'wpIgnoreBlankSummary' => true, 'wpIgnoreBlankArticle' => true, diff --git a/tests/phpunit/includes/api/ApiEditPageTest.php b/tests/phpunit/includes/api/ApiEditPageTest.php index 7a8e208b0c..02d0a0dc57 100644 --- a/tests/phpunit/includes/api/ApiEditPageTest.php +++ b/tests/phpunit/includes/api/ApiEditPageTest.php @@ -513,4 +513,57 @@ class ApiEditPageTest extends ApiTestCase { $this->assertEquals( "testing-nontext", $page->getContentModel() ); $this->assertEquals( $data, $page->getContent()->serialize() ); } + + /** + * This test verifies that after changing the content model + * of a page, undoing that edit via the API will also + * undo the content model change. + */ + public function testUndoAfterContentModelChange() { + $name = 'Help:' . __FUNCTION__; + $uploader = self::$users['uploader']->getUser(); + $sysop = self::$users['sysop']->getUser(); + $apiResult = $this->doApiRequestWithToken( [ + 'action' => 'edit', + 'title' => $name, + 'text' => 'some text', + ], null, $sysop )[0]; + + // Check success + $this->assertArrayHasKey( 'edit', $apiResult ); + $this->assertArrayHasKey( 'result', $apiResult['edit'] ); + $this->assertEquals( 'Success', $apiResult['edit']['result'] ); + $this->assertArrayHasKey( 'contentmodel', $apiResult['edit'] ); + // Content model is wikitext + $this->assertEquals( 'wikitext', $apiResult['edit']['contentmodel'] ); + + // Convert the page to JSON + $apiResult = $this->doApiRequestWithToken( [ + 'action' => 'edit', + 'title' => $name, + 'text' => '{}', + 'contentmodel' => 'json', + ], null, $uploader )[0]; + + // Check success + $this->assertArrayHasKey( 'edit', $apiResult ); + $this->assertArrayHasKey( 'result', $apiResult['edit'] ); + $this->assertEquals( 'Success', $apiResult['edit']['result'] ); + $this->assertArrayHasKey( 'contentmodel', $apiResult['edit'] ); + $this->assertEquals( 'json', $apiResult['edit']['contentmodel'] ); + + $apiResult = $this->doApiRequestWithToken( [ + 'action' => 'edit', + 'title' => $name, + 'undo' => $apiResult['edit']['newrevid'] + ], null, $sysop )[0]; + + // Check success + $this->assertArrayHasKey( 'edit', $apiResult ); + $this->assertArrayHasKey( 'result', $apiResult['edit'] ); + $this->assertEquals( 'Success', $apiResult['edit']['result'] ); + $this->assertArrayHasKey( 'contentmodel', $apiResult['edit'] ); + // Check that the contentmodel is back to wikitext now. + $this->assertEquals( 'wikitext', $apiResult['edit']['contentmodel'] ); + } }